<?php
/*  INSERT INTO `data` VALUES ( 1, 'top', 'Tree Root', 1, 1 )  */
class Data extends Controller {

    function __construct()
	{
		parent::Controller();
        $this->auth->CheckTaMeR(1,100);
	    $this->load->model('nested_sets_model');
        $this->load->model('data_model','data');
        
    }
	
	function index()
	{
        // Simply call the page display method
        $this->_render_page();
    }
    
    function insert_category()
    {
        $category_name = trim($this->input->post('categoryname'));
        $category_data = trim($this->input->post('categorydata'));
        $parentid = (int)$this->input->post('categoryid');
        $parentNode = $this->data->getNodeFromId($parentid);
        
        $fields_array = array(
                            "name"=>$category_name,
                            "data"=>$category_data
                            );
        $this->data->appendNewChild($parentNode,$fields_array);
        
        $this->_render_page();
    }

    function multi_insert()
    {
        $names = trim($this->input->post('categoryname'));
        $parentid = (int)$this->input->post('categoryid');
        $parentNode = $this->data->getNodeFromId($parentid);
        $names = str_replace(', ', ',', $names);
        $names = str_replace(' ,', ',', $names);
        $namearray = explode(",", $names);
        foreach($namearray as $v){
            $fields_array = array("name"=>$v);
            $this->data->appendNewChild($parentNode,$fields_array);
        }
        
        $this->_render_page();
    }

    
    function move_category() 
    {
        // which category to move and where to
        $srcid = (int)$this->input->post('sourceid');
        $dstid = (int)$this->input->post('targetid');
        
        // get the corresponding node data
        $sourceNode = $this->data->getNodeFromId($srcid);
        $targetNode = $this->data->getNodeFromId($dstid);
        
        // now switch on mode to identify how to effect the move
        $mode = $this->input->post('move_mode');
        switch($mode) {
            case "firstchild":
                $method = "setNodeAsFirstChild";
                break;
            case "lastchild":
                $method = "setNodeAsLastChild";
                break;
            case "prevsibling":
                $method = "setNodeAsPrevSibling";
                break;
            case "nextsibling":
                $method = "setNodeAsNextSibling";
                break;
        }
        
        // Now to effect the move
        $this->data->$method($sourceNode,$targetNode);
        
        $this->_render_page();
        
    }
    
    
    function delete_category()
    {
        // get the categoryid of the chosen category
        $deleteid = (int) $this->input->post('deleteid');
        
        // Retrieve the node data for this category
        $targetNode = $this->data->getNodeFromId($deleteid);
        
        // perform the deletion
        $this->data->deleteNode($targetNode);
        
        
        $this->_render_page();
        
    }
    
    // Common output page for the demo
    // Displays the current tree as a space-indented list
    // and three forms for the manipulation of the tree
    function _render_page()
    {
        $data['title'] = 'Data';
        $data['head'] = '';
        $data['sidebar'] = $this->data->getTreeAsHTML();
        $data['categorySelect'] = $this->data->getAllDataSelect("categoryid");
    
        $data['moveCategorySourceSelect'] = $this->data->getAllDataSelect("sourceid");
        $data['moveCategoryTargetSelect'] = $this->data->getAllDataSelect("targetid");
        
        $data['deleteSelect'] = $this->data->getAllDataSelect("deleteid");
        
        $this->load->view('admin/data',$data);
    
    }
    
} // END: class Categories_demo
?>